package com.ima.web.common.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ima.web.beans.db.SubCategories;
import com.ima.web.beans.db.Users;
import com.ima.web.beans.form.CategoriesForm;
import com.ima.web.beans.form.SubCategoriesForm;
import com.ima.web.beans.form.UsersForm;
import com.ima.web.exceptions.UserExistsException;
import com.ima.web.service.RolesService;
import com.ima.web.service.SubCategoriesService;
import com.ima.web.service.UserService;

@Controller
public class Test {

	private UserService userService;
	private SubCategoriesService subCat;
	private RolesService rolesService;
	
	@Autowired
	public void setDepencies(UserService userService, SubCategoriesService subCat,RolesService rolesService){
		this.userService = userService;
		this.subCat = subCat;
		this.rolesService = rolesService;
	}
	
	@RequestMapping("/")
	public String indexController(ModelMap model){
		
		model.put("msg","Hello World");
		
		return "index";
	}
	
	@RequestMapping("/test2")
	public String test2Controller(ModelMap model){
		model.put("msg","Test2  Controller");
		
		return "index";
	}
	
	@RequestMapping("/first/createUser")
	public String createUser(ModelMap model){
		model.put("user", new UsersForm());
		model.put("roles", rolesService.getAllRoles());
		
		return "editUser";
	}
	
	@RequestMapping("/first/saveUser")
	public String saveUser(@ModelAttribute("user") UsersForm uForm,BindingResult result,ModelMap model){
		
		try {
			userService.signUpUser(uForm);
			model.put("msg", "Successfully created user");
		} catch (UserExistsException e) {
			model.put("msg", "Failed to create user");
		}
		
		return "editUser";
	}
	
	@RequestMapping("/jsonTest")
	public @ResponseBody List<SubCategories> testJson(){
//		List<String> a = new ArrayList<String>();
//		a.add("Kebs");
//		a.add("Kebs2");
//		a.add("Kebs3");
		
//		List<SubCategories> sc = new ArrayList<SubCategories>();
//		SubCategories scf = new SubCategories();
//		scf.setName("Test SC 1");
//		scf.setDescription("Test SC 1");
//		scf.setApproved(false);
//		
//		sc.add(scf);
		
		return subCat.getApproved();
	}

}
